from selenium import webdriver
import time
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
# chromedriver_path = r"C:\Program Files\Google\Chrome\Application\chromedriver.exe"
class Grap:
    def __init__(self):
        # 设置驱动的路径,这是 selenium4之后的新写法
        self.service = Service(executable_path=r"C:\Program Files\Google\Chrome\Application\chromedriver.exe")
        # 实例化浏览器对象
        self.driver = webdriver.Chrome(service=self.service)

    # ----- 1.	通过id属性定位元素 -----
    def find_id(self):
        self.driver.get('http://news.baidu.com/')
        # 通过id属性定位元素
        # element = self.driver.find_element_by_id('header-wrapper')
        element = self.driver.find_element(By.ID,'header-wrapper')
        # 访问text属性输出元素的文本内容
        print(element.text)
    
    # ----- 2.	通过class属性定位元素 -----
    def find_class(self):
        self.driver.get("https://movie.douban.com/subject/30279836/comments?status=P")
        # 通过class属性定位元素
        # element = driver.find_element_by_class_name('short')
        element = self.driver.find_element(By.CLASS_NAME,'short')
        print(element.text)

if __name__ == '__main__':
    g = Grap()
    # g.find_id()
    g.find_class()
